<template>
  <div class="blog-view">
    <!-- 博客头部信息 -->
    <div class="blog-header">
      <h1 class="blog-title">{{ blog.title }}</h1>
      <div class="blog-meta">
        <span class="author">
          <el-avatar :src="blog.authorAvatar" :size="24">
            {{ blog.authorName?.charAt(0) }}
          </el-avatar>
          <span class="author-name">{{ blog.authorName }}</span>
        </span>
        <span class="publish-time">{{ formatTime(blog.createdAt) }}</span>
        <span class="category">{{ blog.categoryName }}</span>
      </div>
    </div>

    <!-- 博客内容 -->
    <div class="blog-content">
      <div class="content-text" v-html="blog.content"></div>
    </div>

    <!-- 点赞收藏组件 -->
    <LikeCollectComponent
      :blog-id="blogId"
      :initial-like-count="blog.likeCount || 0"
      :initial-collect-count="blog.collectCount || 0"
      :view-count="blog.viewCount || 0"
      :comment-count="commentCount"
      @like-updated="handleLikeUpdated"
      @collect-updated="handleCollectUpdated"
      ref="likeCollectRef"
    />

    <!-- 评论组件 -->
    <CommentComponent
      :blog-id="blogId"
      @comment-updated="handleCommentUpdated"
      ref="commentRef"
    />

    <!-- 相关推荐 -->
    <div class="related-blogs" v-if="relatedBlogs.length > 0">
      <h3>相关推荐</h3>
      <div class="related-list">
        <div 
          v-for="relatedBlog in relatedBlogs" 
          :key="relatedBlog.id" 
          class="related-item"
          @click="goToBlog(relatedBlog.id)"
        >
          <h4>{{ relatedBlog.title }}</h4>
          <p>{{ relatedBlog.summary }}</p>
          <div class="related-meta">
            <span>{{ relatedBlog.authorName }}</span>
            <span>{{ formatTime(relatedBlog.createdAt) }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getBlogDetail } from '@/api/blog'
import LikeCollectComponent from '@/components/LikeCollectComponent.vue'
import CommentComponent from '@/components/CommentComponent.vue'

const route = useRoute()
const router = useRouter()

// 博客ID
const blogId = computed(() => route.params.id)

// 博客数据
const blog = ref({
  id: '',
  title: '',
  content: '',
  authorName: '',
  authorAvatar: '',
  createdAt: '',
  categoryName: '',
  likeCount: 0,
  collectCount: 0,
  viewCount: 0
})

// 相关博客
const relatedBlogs = ref([])

// 评论数量
const commentCount = ref(0)

// 组件引用
const likeCollectRef = ref(null)
const commentRef = ref(null)

// 加载博客详情
const loadBlogDetail = async () => {
  try {
    const res = await getBlogDetail(blogId.value)
    if (res.success) {
      blog.value = res.data
      // 更新点赞收藏组件的统计数据
      if (likeCollectRef.value) {
        likeCollectRef.value.updateStats(
          blog.value.likeCount || 0,
          blog.value.collectCount || 0
        )
      }
    } else {
      ElMessage.error('博客不存在或已被删除')
      router.push('/blog')
    }
  } catch (error) {
    ElMessage.error('获取博客详情失败')
    router.push('/blog')
  }
}

// 处理点赞更新
const handleLikeUpdated = (data) => {
  blog.value.likeCount = data.count
}

// 处理收藏更新
const handleCollectUpdated = (data) => {
  blog.value.collectCount = data.count
}

// 处理评论更新
const handleCommentUpdated = () => {
  // 可以在这里更新评论数量或做其他处理
  commentCount.value++
}

// 跳转到其他博客
const goToBlog = (id) => {
  router.push(`/blog/${id}`)
}

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  const date = new Date(time)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  })
}

onMounted(() => {
  loadBlogDetail()
})
</script>

<style scoped>
.blog-view {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.blog-header {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.blog-title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #333;
  margin-bottom: 15px;
  line-height: 1.3;
}

.blog-meta {
  display: flex;
  align-items: center;
  gap: 20px;
  color: #666;
  font-size: 14px;
}

.author {
  display: flex;
  align-items: center;
  gap: 8px;
}

.author-name {
  font-weight: 500;
  color: #333;
}

.publish-time,
.category {
  color: #999;
}

.blog-content {
  margin-bottom: 30px;
  line-height: 1.8;
  font-size: 16px;
  color: #333;
}

.content-text {
  word-wrap: break-word;
}

.content-text :deep(h1),
.content-text :deep(h2),
.content-text :deep(h3),
.content-text :deep(h4),
.content-text :deep(h5),
.content-text :deep(h6) {
  margin: 20px 0 10px 0;
  font-weight: 600;
  color: #333;
}

.content-text :deep(p) {
  margin: 15px 0;
}

.content-text :deep(blockquote) {
  margin: 20px 0;
  padding: 15px 20px;
  background: #f8f9fa;
  border-left: 4px solid #1890ff;
  color: #666;
}

.content-text :deep(code) {
  background: #f1f3f4;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
}

.content-text :deep(pre) {
  background: #f8f9fa;
  padding: 15px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 20px 0;
}

.content-text :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  margin: 15px 0;
}

.related-blogs {
  margin-top: 40px;
  padding-top: 30px;
  border-top: 1px solid #eee;
}

.related-blogs h3 {
  font-size: 1.5rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
}

.related-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.related-item {
  padding: 15px;
  border: 1px solid #eee;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.related-item:hover {
  border-color: #1890ff;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.1);
}

.related-item h4 {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.4;
}

.related-item p {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.related-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .blog-view {
    padding: 15px;
  }
  
  .blog-title {
    font-size: 2rem;
  }
  
  .blog-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .related-list {
    grid-template-columns: 1fr;
  }
}
</style>